$(function() {
	// 修改导航
	$("#navi > ul").empty().html("<li>首页</li>-><li>指标管理</li>-><li>已初审</li>");

	//
	// 取得所有的公共信息
	//
	init_common_info("cindex/listIndexNo.action", "#index_no");
	init_common_info("cindex/listPaperDate.action", "#paper_date");
	init_common_info("cindex/listBudgetUnit.action", "#budget_unit");
	init_common_info("cindex/listFundProp.action", "#fund_prop");
	init_common_info("cindex/listBudgetItem.action", "#budget_item");
	init_common_info("cindex/listIndexSource.action", "#index_source");
	init_common_info("cindex/listIndexType.action", "#index_type");
	init_common_info("cindex/listPayMethod.action", "#pay_method");
	init_common_info("cindex/listFuncType.action", "#func_type");
	init_common_info("cindex/listFinancialType.action", "#financial_type");

	init_index_jqgrid_data();
	
	// 终审审核处理
	audit_init();
	
	// 搜索
	$("#search_index_submit").click(function() {
		do_search();
	});
	
	//清除搜索条件
	$("#search_index_reset").click(function() {
		$("#index_form select").val(""); 
		do_search();
	});
});

function init_index_jqgrid_data() {

	$("#index_data")
			.jqGrid(
					{
						url : "index/auditIndexInfo.action",
						datatype : "json",
						mtype : "post",
						height : 300,
						width : 1188,
						colNames : [ '序号', '状态', '录入时间', '指标文号', '发文标题', '发文日期',
								'预算项目', '预算单位', '金额', '剩余金额', '操作', '---', '---','---' ],
						colModel : [ {
							name : '---',
							index : '---',
							width : 30,
							align : "center"
						},{
							name : '*status',
							index : '*status',
							width : 30,
							align : "center"
						}, {
							name : 'createTime',
							index : 'createTime',
							width : 40,
							align : "center",
						}, {
							name : 'indexNo',
							index : 'indexNo',
							width : 50,
							align : "center"
						}, {
							name : 'paperTitle',
							index : 'paperTitle',
							width : 110,
							align : "left"
						}, {
							name : 'paperDate',
							index : 'paperDate',
							width : 40,
							align : "center",
						}, {
							name : 'budgetItem',
							index : 'budgetItem',
							width : 40,
							align : "center",
						}, {
							name : 'budgetUnit',
							index : 'budgetUnit',
							width : 60,
							align : "center",
						}, {
							name : 'money',
							index : 'money',
							width : 40,
							align : "right",
						},  {
							name : 'spareMoney',
							index : 'spareMoney',
							width : 40,
							align : "right",
						}, {
							name : 'oper',
							index : 'oper',
							width : 60,
							align : "center",
						},{
							name : 'budgetUnitNo',
							index : 'budgetUnitNo',
							align : "center",
							hidden: true
						}, {
							name : 'budgetItemNo',
							index : 'budgetItemNo',
							hidden: true
						}, {
							name : 'status',
							index : 'staus',
							hidden: true
						}],

						// rownumbers: true,
						multiselect: true,
						// caption: "指标文号信息",
						rowNum : 10,
						rowList : [ 10, 20, 30 ],
						pager : '#index_data_pager',
						// sortname: 'id',
						recordpos : 'right',
						viewrecords : true,
						prmNames : {
							search : "pageInfo.search",
							page : "pageInfo.page",
							rows : "pageInfo.rows",
							sidx : "pageInfo.sidx",
							sord : "pageInfo.sord",
							nd : "pageInfo.nd",
						},
						jsonReader : {
							root : "indexes",
							page : "pageInfo.page",
							records : "pageInfo.record",
							total : "pageInfo.totalPage",
							sidx : "pageInfo.sidx",
							sord : "pageInfo.sord",
							userdata : "",
							repeatitems : false
						},

						afterInsertRow : function(rowid, rowdata, rowelem) {
							
							var _deptNo = window.User.deptNo;
							var _postNo = window.User.postNo;
							
							// 增加操作列的信息
							var oper_info = "<a class='operation' href='javascript:void(0)' onclick=info_data_row('"
									+ rowdata["indexNo"] + "','" + rowdata['budgetUnitNo'] +  "','" + rowdata["budgetItemNo"]
									+ "')>指标详情</a>";

							if(  _deptNo >= "002" && _deptNo <="008" && _deptNo != "003" && _postNo == "002" ){
								oper_info = "<a class='operation' href='javascript:void(0)' onclick=info_data_row('"
									+ rowdata["indexNo"] + "','" + rowdata['budgetUnitNo'] +  "','" + rowdata["budgetItemNo"]
									+ "')>指标详情</a>|"
									+"<a class='operation' href='javascript:void(0)' onclick=modify_data_row('"
									+ rowdata["indexNo"] + "','" + rowdata['budgetUnitNo'] +  "','" + rowdata["budgetItemNo"]
									+ "')>修改</a>|"
									+ "<a class='operation' href='javascript:void(0)' onclick=delete_data_row('"
									+ rowdata["indexNo"] + "','" + rowdata['budgetUnitNo'] +  "','" + rowdata["budgetItemNo"]
									+ "')>删除</a>";
							}
							
							var _status = window.STATUS_MAP.index[rowdata["status"]];
							$("#index_data").setRowData(rowid, {
								'---' : rowid,
								'*status': _status,
								oper : oper_info
							});
						}
					});
}

function delete_data_row(indexNo, budgetUnit, budgetItem) {
	var confirm = window.confirm("你确定要删除吗?");
	if (confirm) {
		$.ajax({
			type : "post",
			url : "index/deleteIndex.action",
			data : {
				"idx.indexNo" : indexNo,
				"idx.budgetUnitNo" : budgetUnit,
				"idx.budgetItemNo" : budgetItem
			},

			success : function(data, textStatus) {
				$("#index_data").trigger("reloadGrid");
			},

			complete : function(XMLHttpRequest, textStatus) {
			},
			error : function() {
			}
		});
	}
}

//修改指标相关信息
function modify_data_row(indexNo, budgetUnit, budgetItem) {
	$.ajax({
		type : "post",
		url : "index/preModifyIndex.action",
		data : {
			"idx.indexNo" : indexNo,
			"idx.budgetUnitNo" : budgetUnit,
			"idx.budgetItemNo" : budgetItem
		},

		success : function(data, textStatus) {
			$("#data_area").toggle();
			$("#data_area_sub").toggle();
			$("#data_area_sub").empty().html(data);
		},

		complete : function(XMLHttpRequest, textStatus) {
		},
		error : function() {
		}
	});
}

//查看指标相关详细信息
function info_data_row(indexNo, budgetUnit, budgetItem) {
	$.ajax({
		type : "post",
		url : "index/detailIndex.action",
		data : {
			"idx.indexNo" : indexNo,
			"idx.budgetUnitNo" : budgetUnit,
			"idx.budgetItemNo" : budgetItem
		},

		success : function(data, textStatus) {
			$("#data_area").toggle();
			$("#data_area_sub").toggle();
			$("#data_area_sub").empty().html(data);
		},

		complete : function(XMLHttpRequest, textStatus) {
		},
		error : function() {
		}
	});
}

//查询数据
function do_search() {
	//var nm_mask = jQuery("#item_nm").val();
	//var cd_mask = jQuery("#search_cd").val();
	//jQuery("#bigset").jqGrid('setGridParam',{url:"bigset.php?nm_mask="+nm_mask+"&cd_mask="+cd_mask,page:1}).trigger("reloadGrid");

	var index_no = $("#index_no").val();
	var paper_date = $("#paper_date").val();
	var budget_unit = $("#budget_unit").val();
	var index_source = $("#index_source").val();
	var index_type = $("#index_type").val();
	var fund_prop = $("#fund_prop").val();
	var pay_method = $("#pay_method").val();
	var budget_item = $("#budget_item").val();
	var func_type = $("#func_type").val();
	var financial_type = $("#financial_type").val();
	//	var settle_method = $("#settle_method").val();
	//	var budget_type = $("#budget_type").val();
	//
	//	var money = $("#money").val();
	//	var purpose = $("#purpose").val();

	var _postData = {
		"cond.indexNo" : index_no,
		"cond.paperDate": paper_date,
		"cond.budgetUnit" : budget_unit,
		"cond.indexSource" : index_source,
		"cond.indexType" : index_type,
		"cond.fundProp" : fund_prop,
		"cond.payMethod" : pay_method,
		"cond.budgetItem" : budget_item,
		"cond.funcType" : func_type,
		"cond.financialType" : financial_type,
	//"idx.settleMethod":settle_method,
	//"idx.budgetType":budget_type,
	};
	$("#index_data").setGridParam({
		page : 1,
		postData : _postData
	}).trigger("reloadGrid");
}

//终审审核功能处理
function audit_init() {

	// 申核通过功能处理
	$("#audit_ok").click(function() {
		var data = $("#index_data");
		var select = data.jqGrid('getGridParam', 'selarrrow');
		var idList = "";
		$.each(select, function(i, n) {
			var ret = data.jqGrid('getRowData', n);
			idList += ret.indexNo + ":" + ret.budgetUnitNo + ":" + ret.budgetItemNo + ";";
		});

		// 送审
		$.ajax({
			type : "post",
			url : "index/approveFinalAudit.action",
			data : {
				"idList" : idList
			},

			success : function(data, textStatus) {
				$("#index_data").trigger("reloadGrid");
			},

			complete : function(XMLHttpRequest, textStatus) {
			},
			error : function() {
			}
		});

	});
	
	// 申核不通过功能处理
	$("#audit_nok").click(function() {
		var data = $("#index_data");
		var select = data.jqGrid('getGridParam', 'selarrrow');
		var idList = "";
		$.each(select, function(i, n) {
			var ret = data.jqGrid('getRowData', n);
			idList += ret.indexNo + ":" + ret.budgetUnitNo + ":" + ret.budgetItemNo + ";";
		});

		// 送审
		$.ajax({
			type : "post",
			url : "index/rejectFinalAudit.action",
			data : {
				"idList" : idList
			},

			success : function(data, textStatus) {
				$("#index_data").trigger("reloadGrid");
			},

			complete : function(XMLHttpRequest, textStatus) {
			},
			error : function() {
			}
		});

	});
}